package com.cq.hd.order.render.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cq.hd.common.enums.biz.ActiveTypeEnum;
import com.cq.hd.common.enums.biz.OrderItemTypeEnum;
import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.order.render.OrderRenderStepEnum;
import com.cq.hd.order.render.dto.ActiveGoodsDto;
import com.cq.hd.order.render.dto.PriceDetailDto;
import com.cq.hd.product.api.dto.ActiveGoodsSonDto;
import com.cq.hd.product.api.vo.ActiveVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品渲染（把报名签到也当作是一个商品）
 *
 * @author much
 * @since 2024/09/15 14:28
 */
@Component
public class ActiveGoodsRender extends AbstractRenderStep {

    @Override
    public OrderRenderStepEnum step() {
        return OrderRenderStepEnum.ACTIVE_GOODS;
    }

    @Override
    public void render(OrderContext orderContext) {
        ActiveVo activeVo = orderContext.getActiveVo();
        if (activeVo.getActiveType().equals(ActiveTypeEnum.GROUP.getValue())) {
            renderGoodsForGoodsOnly(orderContext);
        } else if (activeVo.getActiveType().equals(ActiveTypeEnum.ENROLL.getValue())) {
            renderGoodsForEnrollOnly(orderContext);
        } else if (activeVo.getActiveType().equals(ActiveTypeEnum.GROUP_ENROLL.getValue())) {
            renderGoodsForGoodsAndEnroll(orderContext);
        }
        // 设置商品总价
        setTotalGoodsPrice(orderContext);
    }

    private void renderGoodsForGoodsOnly(OrderContext orderContext) {
        ActiveVo activeVo = orderContext.getActiveVo();
        List<ActiveGoodsDto> goodsList = new ArrayList<>();
        for (ActiveGoodsSonDto activeGoodsSonDto : activeVo.getActiveGoods()) {
            ActiveGoodsDto activeGoodsDto = BeanUtil.copyProperties(activeGoodsSonDto, ActiveGoodsDto.class);
            activeGoodsDto.setType(OrderItemTypeEnum.GOODS.getValue());
            goodsList.add(activeGoodsDto);
        }
        orderContext.getActiveGoods().addAll(goodsList);
    }

    private void renderGoodsForEnrollOnly(OrderContext orderContext) {
        ActiveVo activeVo = orderContext.getActiveVo();
        if (activeVo.getActiveType().equals(ActiveTypeEnum.ENROLL.getValue())) {
            PriceDetailDto priceDetailDTO = orderContext.getPriceDetailDTO();
            activeVo.setSignInFee(priceDetailDTO.getActivePrice());
        }

        ActiveGoodsDto activeGoodsDto = new ActiveGoodsDto();
        activeGoodsDto.setType(OrderItemTypeEnum.SIGN_IN.getValue());
        activeGoodsDto.setGoodsPrice(activeVo.getSignInFee());
        activeGoodsDto.setGoodsNum(1);
        activeGoodsDto.setMerchantId(orderContext.getBusinessMerchantId());
        activeGoodsDto.setGoodsId("");
        activeGoodsDto.setGoodsName(activeVo.getActiveTitle());
        activeGoodsDto.setGoodsImgs(activeVo.getActiveImg());

        orderContext.getActiveGoods().add(activeGoodsDto);
    }

    private void renderGoodsForGoodsAndEnroll(OrderContext orderContext) {
        renderGoodsForGoodsOnly(orderContext);
        renderGoodsForEnrollOnly(orderContext);
    }

    private void setTotalGoodsPrice(OrderContext orderContext) {
        // 设置商品总价
        PriceDetailDto priceDetailDto = orderContext.getPriceDetailDTO();
        priceDetailDto.reCalculateTotalGoodsPrice(orderContext);
//        BigDecimal totalGoodsPrice = orderContext.getActiveGoods().stream()
//                .map(e -> e.getGoodsPrice().multiply(BigDecimal.valueOf(e.getGoodsNum())))
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        CreateOrderDto createOrderDto = orderContext.getCreateOrderDto();
//        // 再乘以订单份数
//        totalGoodsPrice = totalGoodsPrice.multiply(BigDecimal.valueOf(createOrderDto.getOrderNum()));
//        priceDetailDto.setTotalGoodsPrice(totalGoodsPrice);
    }

}
